package com.gzyj.server.factory.jtt808.up;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.gzyj.data.common.VehicleIncrement;
import com.gzyj.jtt808.common.DriverInfo;
import com.gzyj.jtt808.v2013.HeaderMsgBodyProperty;
import com.gzyj.jtt808.v2013.Package;
import com.gzyj.jtt808.v2013.enums.JT808TerminalResult;
import com.gzyj.jtt808.v2013.protocol.JTT808_0x0702;
import com.gzyj.jtt808.v2013.protocol.JTT808_0x8001;
import com.gzyj.server.factory.CommonHandler;
import com.gzyj.server.factory.jtt808.NetWorkSerrvice;
import io.netty.channel.ChannelHandlerContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.stereotype.Service;

/**
 * @author lhj
 */
@Service
public class JTT808_0x0702_Handler implements CommonHandler {

    @Autowired
    private NetWorkSerrvice netWorkSerrvice;

    @Autowired
    private KafkaTemplate<String, Object> kafkaTemplate;

    @Value("${kafka.queue.driver}")
    private String kafka_topic_driver;

    @Override
    public void handler(ChannelHandlerContext ctx, Package msg) throws Exception {
        JTT808_0x0702 jtt808_0x0702 = (JTT808_0x0702) msg;

        JTT808_0x8001 jtt808_0x8001 = new JTT808_0x8001();

        HeaderMsgBodyProperty headerMsgBodyProperty = new HeaderMsgBodyProperty();
        headerMsgBodyProperty.setSubcontract(msg.getPackageHeader().getMsgBodyProperty().isSubcontract());
        headerMsgBodyProperty.setEncryptMethod(msg.getPackageHeader().getMsgBodyProperty().getEncryptMethod());
        jtt808_0x8001.getPackageHeader().setMsgBodyProperty(headerMsgBodyProperty);
        jtt808_0x8001.getPackageHeader().setTerminalPhoneNo(msg.getPackageHeader().getTerminalPhoneNo());

        jtt808_0x8001.setMsgNum(msg.getPackageHeader().getMsgNum());
        jtt808_0x8001.setAckMsgId(msg.getPackageHeader().getMsgId());
        jtt808_0x8001.setResult(JT808TerminalResult.成功_确认);

        netWorkSerrvice.send2Veh(jtt808_0x8001.getPackageHeader().getTerminalPhoneNo(), jtt808_0x8001.encode());

        DriverInfo driverInfo = jtt808_0x0702.transform();

        kafkaTemplate.send(kafka_topic_driver, "kafka_topic_driver", JSON.toJSONString(driverInfo, SerializerFeature.DisableCircularReferenceDetect));


    }

    @Override
    public void handler(ChannelHandlerContext ctx, com.gzyj.jtt808.v2019.Package msg) throws Exception {
        com.gzyj.jtt808.v2019.protocol.JTT808_0x0702 jtt808_0x0702 = (com.gzyj.jtt808.v2019.protocol.JTT808_0x0702) msg;

        com.gzyj.jtt808.v2019.protocol.JTT808_0x8001 jtt808_0x8001 = new com.gzyj.jtt808.v2019.protocol.JTT808_0x8001();
        com.gzyj.jtt808.v2019.HeaderMsgBodyProperty headerMsgBodyProperty = new com.gzyj.jtt808.v2019.HeaderMsgBodyProperty();

        headerMsgBodyProperty.setSubcontract(jtt808_0x0702.getPackageHeader().getMsgBodyProperty().isSubcontract());
        headerMsgBodyProperty.setEncryptMethod(jtt808_0x0702.getPackageHeader().getMsgBodyProperty().getEncryptMethod());
        headerMsgBodyProperty.setVersionFlag(jtt808_0x0702.getPackageHeader().getMsgBodyProperty().isVersionFlag());
        jtt808_0x8001.getPackageHeader().setMsgBodyProperty(headerMsgBodyProperty);
        jtt808_0x8001.getPackageHeader().setTerminalPhoneNo(jtt808_0x0702.getPackageHeader().getTerminalPhoneNo());
        jtt808_0x8001.getPackageHeader().setProtocolVersion(jtt808_0x0702.getPackageHeader().getProtocolVersion());

        jtt808_0x8001.setMsgNum(jtt808_0x0702.getPackageHeader().getMsgNum());
        jtt808_0x8001.setAckMsgId(jtt808_0x0702.getPackageHeader().getMsgId());
        jtt808_0x8001.setResult(JT808TerminalResult.成功_确认);

        netWorkSerrvice.send2Veh(jtt808_0x8001.getPackageHeader().getTerminalPhoneNo(), jtt808_0x8001.encode());

        DriverInfo driverInfo = jtt808_0x0702.transform();

        kafkaTemplate.send(kafka_topic_driver, "kafka_topic_driver", JSON.toJSONString(driverInfo, SerializerFeature.DisableCircularReferenceDetect));
    }

    @Override
    public void handler(ChannelHandlerContext ctx, com.gzyj.jtt808.v2011.Package msg) {

    }

    @Override
    public void handler(VehicleIncrement vehicleInfo, String msg) {

    }
}
